<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="8" v-for="product in filteredProducts" :key="product.id">
        <ProductItem :product="product" />
      </el-col>
    </el-row>
    <!-- 筛选组件 -->
    <el-form inline>
      <el-form-item label="价格区间">
        <el-input-number v-model="filters.priceMin" @change="applyFilters" :min="0"></el-input-number>
        -
        <el-input-number v-model="filters.priceMax" @change="applyFilters" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item label="名称">
        <el-input v-model="filters.name" @input="applyFilters"></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import ProductItem from './ProductItem.vue';
import { mapState } from 'vuex';
import _ from 'lodash';

export default {
  name: 'ProductList',
  components: {
    ProductItem
  },
  data() {
    return {
      filters: {
        priceMin: 0,
        priceMax: Infinity,
        name: '',
      },
    };
  },
  computed: {
    ...mapState(['products']),
    filteredProducts() {
      return this.products.filter(product => {
        const priceOk = product.price >= this.filters.priceMin && product.price <= this.filters.priceMax;
        const nameOk = product.name.toLowerCase().includes(this.filters.name.toLowerCase());
        return priceOk && nameOk;
      });
    },
  },
  methods: {
    applyFilters() {
      // 筛选逻辑已经在 computed 中实现
    },
  },
  created() {
    this.debouncedSearch = _.debounce(this.debouncedSearch, 300);
  },
};
</script>